﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web.Configuration;
using log4net;

namespace TheBullysStore.Models.ModuloAdministracion.DepartamentoPeru
{
    public class DepartamentoPeruDAO
    {
        public static String cadenaDB = WebConfigurationManager.ConnectionStrings["TheBullysStore"].ConnectionString;
        private static ILog log = LogManager.GetLogger(typeof(DepartamentoPeruDAO));

        public List<DepartamentoPeruBean> ListarDepartamentoPeru(DepartamentoPeruBean departamentoPeru)
        {
            SqlConnection sqlCon = null;
            try
            {
                List<DepartamentoPeruBean> listaDepartamentoPeru = new List<DepartamentoPeruBean>();

                sqlCon = new SqlConnection(cadenaDB);

                sqlCon.Open();

                string commandString = "SELECT * FROM Departamento_Lugar"; 

                SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);

                SqlDataReader dataReader = sqlCmd.ExecuteReader();

                while (dataReader.Read()){
                        DepartamentoPeruBean DepartamentoPeru = new DepartamentoPeruBean();
                        DepartamentoPeru.Nombre = Convert.ToString(dataReader["Nombre"]);
                        DepartamentoPeru.IdDepartamento = Convert.ToInt32(dataReader["Id_Departamento"]);
                        listaDepartamentoPeru.Add(DepartamentoPeru);
                }
                dataReader.Close();

                return listaDepartamentoPeru;
            }
            catch (Exception ex)
            {
                log.Error("listarDepartamentoPeru(EXCEPTION): ", ex);
                throw ex;
            }
            finally
            {
                sqlCon.Close();
            }
        }

        public List<DepartamentoPeruBean> ListarDepartamentoProv(DepartamentoPeruBean departamento)
        {
            SqlConnection sqlCon = null;

            try
            {
                List<DepartamentoPeruBean> listaDepartamentoPeru = new List<DepartamentoPeruBean>();

                sqlCon = new SqlConnection(cadenaDB);

                sqlCon.Open();

                string commandString = "SELECT d.id_departamento, d.nombre FROM departamento_lugar d, edificio e where e.id_edificio = " + departamento.IdEdificio +
                                       " AND d.id_departamento = e.id_departamento";

                SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);

                SqlDataReader dataReader = sqlCmd.ExecuteReader();

                while (dataReader.Read())
                {
                    DepartamentoPeruBean DepartamentoPeru = new DepartamentoPeruBean();
                    DepartamentoPeru.Nombre = Convert.ToString(dataReader["nombre"]);
                    DepartamentoPeru.IdDepartamento = Convert.ToInt32(dataReader["id_departamento"]);
                    listaDepartamentoPeru.Add(DepartamentoPeru);
                }

                for (int i = 0; i < listaDepartamentoPeru.Count(); i++)
                {
                    if (listaDepartamentoPeru[i].IdDepartamento != 15)
                    {
                        DepartamentoPeruBean DepPeru = new DepartamentoPeruBean();
                        DepPeru.Nombre = "LIMA";
                        DepPeru.IdDepartamento = 15;
                        listaDepartamentoPeru.Add(DepPeru);
                    }
                }

                dataReader.Close();


                return listaDepartamentoPeru;
            }
            catch (Exception ex)
            {
                log.Error("listarDepartamentoProv(EXCEPTION): ", ex);
                throw ex;
            }
            finally {
                sqlCon.Close();
            }
        }
    }
}